# -*- coding: utf-8 -*-
# created on 2016/01/11

from mathsolver.functions.base import BaseFunction, BaseBelong, BaseIneq
from mathsolver.functions.daoshu.daoshuyunsuan import HanShuDaoShuYunSuan
from mathsolver.functions.hanshu.helper import check_func, check_inter
from mathsolver.functions.hanshu.hengchengli import HengChengLiRenYiXing


class DanDiaoXingDaoShuQiuCan(BaseFunction):
    """已知函数在区间上的单调性求参数-导数"""

    def solver(self, *args):
        func, inter, dandiao = check_func(args[0]), check_inter(args[1]), args[2]

        if '增' in dandiao:
            rel = '>='
        elif '减' in dandiao:
            rel = '<='
        else:
            raise ValueError('unknow type %s' % dandiao)

        # 求函数导数
        deriv = HanShuDaoShuYunSuan().solver(func).output[0]
        self.steps.append(["", "对函数求导得 {}".format(deriv.printing())])

        # 单调问题转化为恒成立问题
        self.steps.append(["函数 %s 在 %s 上单调递%s" % (func.printing(), str(inter), dandiao),
                           "所以 %s %s 0 在 %s 上恒成立" % (deriv.printing(), rel, str(inter))])

        renyixing_solver = HengChengLiRenYiXing().solver(
            BaseIneq([deriv.expression, rel, '0']), BaseBelong([func.var, "\\in", inter]))
        self.steps.extend(renyixing_solver.steps)
        self.output.extend(renyixing_solver.output)
        self.label.add("已知函数在区间上的单调性求参数-导数")
        return self


if __name__ == '__main__':
    pass
